﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

namespace Lamex.mLabor.UI
{
    public partial class frmLBR_LaborTime_Query : frmBaseChild
    {
        //string StrSql;
        string SubFactory, Workstation, WorkCenter;

        public frmLBR_LaborTime_Query()
        {
            InitializeComponent();
        }

        private void frmLBR_WorkHour_Query_Load(object sender, EventArgs e)
        {
            dateEdit_Start.EditValue = DateTime.Now;
        }

        private void frmLBR_WorkHour_Query_Resize(object sender, EventArgs e)
        {
            splitContainer1.SplitterDistance = this.Width / 3;
            splitContainer2.SplitterDistance = this.Width / 3;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            gridView1.Columns.Clear();
            gridView2.Columns.Clear();
            gridView3.Columns.Clear();

            getGridViewData_Query();
        }

        private void getGridViewData_Query()
        {
            getQueyCondition();

            string strDate_Start = dateEdit_Start.Text.ToString();
            string strDate_End = dateEdit_End.Text.ToString();

            string StrSql1 = getSqlString_Query_12("HR_Work_Hours_By_SubFactory_Cancel", "SUB_FACTORY AS 分厂,", SubFactory, strDate_Start, strDate_End);
            string StrSql2 = getSqlString_Query_12("HR_Work_Hours_By_Workstation_Cancel", "WORKSTATION AS 工作站,", Workstation, strDate_Start, strDate_End);
            string StrSql3 = getSqlString_Query_12("HR_Work_Hours_By_WorkCenter_Cancel", "WORK_CENTER AS 工作中心,", WorkCenter, strDate_Start, strDate_End);
            //string StrSql3 = getSqlString_Query_3(WorkCenter, strDate_Start, strDate_End);

            setGridViewData(StrSql1, gridControl1, gridView1);
            setGridViewData(StrSql2, gridControl2, gridView2);
            setGridViewData(StrSql3, gridControl3, gridView3);
        }

        //同工时维护不同，即可查询全部，多了default子句
        private void getQueyCondition()
        {
            string strSub_Factory = "";
            if (comboBox1.SelectedIndex != -1)
            {
                strSub_Factory = comboBox1.SelectedItem.ToString();
            }

            switch (strSub_Factory)
            {
                case "生产-桌子":
                    SubFactory = " AND SUB_FACTORY='生产-桌子' ";
                    Workstation = " AND WORKSTATION LIKE 'D%' ";
                    WorkCenter = " AND WORK_CENTER LIKE 'D%' ";
                    break;
                case "生产-椅子":
                    SubFactory = " AND SUB_FACTORY='生产-椅子' ";
                    Workstation = " AND WORKSTATION LIKE 'S%' ";
                    WorkCenter = " AND WORK_CENTER LIKE 'S%' ";
                    break;
                case "生产-屏风":
                    SubFactory = " AND SUB_FACTORY='生产-屏风' ";
                    Workstation = " AND WORKSTATION LIKE 'P%' ";
                    WorkCenter = " AND WORK_CENTER LIKE 'P%' ";
                    break;
                case "生产-MCG":
                    SubFactory = " AND SUB_FACTORY='生产-MCG' ";
                    Workstation = " AND WORKSTATION LIKE 'M%' ";
                    WorkCenter = " AND (WORK_CENTER LIKE 'M%' OR WORK_CENTER LIKE 'T0%') ";
                    break;
                default:
                    SubFactory = " AND 1=1 ";
                    Workstation = " AND 1=1 ";
                    WorkCenter = " AND 1=1 ";
                    break;
            }
        }

        private string getSqlString_Query_12(string strDataTable, string strCurField, string strCondition, string dateStart, string dateEnd)
        {
            string StrSql = "";
            if (dateEnd == "")
            {
                StrSql = @"SELECT WORKING_DATE AS 工作日," + strCurField + @"WORKING_HOURS AS 实际工时,
                            UPDATE_TIME AS 更新时间,UPDATE_USER AS 更新用户
                            FROM " + strDataTable + @"
                            WHERE WORKING_DATE='" + dateStart + @"'" + strCondition;
            }
            else
            {
                StrSql = @"SELECT WORKING_DATE AS 工作日," + strCurField + @"WORKING_HOURS AS 实际工时,
                            UPDATE_TIME AS 更新时间,UPDATE_USER AS 更新用户
                            FROM " + strDataTable + @"
                            WHERE WORKING_DATE>='" + dateStart + @"' AND WORKING_DATE<='" + dateEnd + @"'" + strCondition;
            }
            return StrSql;
        }

        private string getSqlString_Query_3(string strCondition, string dateStart, string dateEnd)
        {
            string StrSql;
            if (dateEnd == "")
            {
                StrSql = @" WHERE WORKING_DATE='" + dateStart + @"'" + strCondition;
            }
            else
            {
                StrSql = @" WHERE WORKING_DATE>='" + dateStart + @"' AND WORKING_DATE<='" + dateEnd + @"'" + strCondition;
            }

            StrSql = @"						SELECT WORKING_DATE AS 工作日,WORK_CENTER AS 工作中心,b.PROCESS_NAME AS 工序描述,
						WORKING_HOURS AS 实际工时,a.UPDATE_TIME AS 更新时间,a.UPDATE_USER AS 更新用户
                        FROM
						(SELECT *
                        FROM HR_Work_Hours_By_WorkCenter_Cancel
                        WHERE WORKING_DATE IN
                        (SELECT MAX(WORKING_DATE)
                        FROM HR_Work_Hours_By_WorkCenter_Cancel"
                        + StrSql + @")) AS a
						LEFT JOIN 
						IE_WorkCenter_Definition AS b
						ON a.WORK_CENTER=b.PROCESS_CODE";
            return StrSql;
        }

        private void setGridViewData(string StrSql, GridControl gc, GridView gv)
        {
            DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(StrSql);
            //dt.Rows.Clear();
            if (dt.Rows.Count > 0)
            {
                gc.DataSource = dt;
                //setGridViewColumnAutoWidth(gv);

                gv.OptionsBehavior.Editable = false;
                gv.OptionsView.ColumnAutoWidth = true;
                //gv.Columns[0].BestFit();
                //gv.Columns[1].BestFit();

                //gridView3.BestFitColumns();
                //setGridViewColumnAutoWidth();

                //refreshPositionStaduss();
            }
        }

        private void 隐藏选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 0;
        }

        private void 显示选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 60;
        }

        //gridView仅复制单元格内容，复制选中行焦点单元格的值
        private void gridView3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control & e.KeyCode == Keys.C)
            {
                Clipboard.SetDataObject(gridView3.GetFocusedRowCellValue(gridView3.FocusedColumn));
                e.Handled = true;
            }

            //或设置属性
            //optionBehavior - CopyToClipboardWithColumnHeader = false;
            //optionSelection - MultiSelectMode = CellSelect;
        }
    }
}